import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import rmi.Connection;
import rmi.ConnectionInterface;
import rmi.Discussion;
import rmi.Me;
import rmi.Message;
import rmi.UserInterface;

/**
 * @author REMY Timothée and CARTIER Arthur
 * 
 */
public class testEnvoisMessageEmmeteur {

	public static void main(final String[] args) throws RemoteException,
			MalformedURLException, NotBoundException, InterruptedException,
			AlreadyBoundException {
		final Registry r = LocateRegistry.createRegistry(2001);
		final Me me = new Me("ArthurEmmeteur", "localhost", 2001);
		// Thread.sleep(2000);

		final Connection c = new Connection(2001, r, me);
		r.bind("connection", c);

		final ConnectionInterface timC = (ConnectionInterface) Naming
				.lookup("//localhost:1099/connection");

		timC.connect(me, c);

		// Thread.sleep(2000);

		System.out.println("Amis connecté : " + me.getFriends().size());
		for (final UserInterface friend : me.getFriends().values()) {
			System.out.println(friend.getPseudo());
		}

		Discussion d = null;
		try {
			d = new Discussion(me);
		} catch (final Exception e) {
			System.err.println("Erreur lors de la création de la discussion");
			e.printStackTrace();
		}

		// d.getListDiscussions().add(d);

		me.getFriends().values().iterator().next().initDiscussion(d);
		Thread.sleep(2000);
		System.out.println("Amis participant à la discussion : "
				+ d.getParticipants().size());
		for (final UserInterface participant : d.getParticipants()) {
			System.out.println(participant.getPseudo());
		}

		d.sendMessage("Message1", me);

		System.out.println("Message");

		for (final Message mes : d.getListMessages()) {
			System.out.println("Message id :" + mes.getId() + "\n"
					+ mes.getMessage());
		}

	}

}
